﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace Smile.Xaml.Converter
{
    /// <summary>Bytes to image converter.</summary>
    public class BytesToImageConverter : IValueConverter
    {
        // @Public
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return new BitmapImage();
            }

            try
            {
                var bytes = (byte[])value;
                var ms = new MemoryStream(bytes);
                var image = new BitmapImage();
                image.SetSource(ms);
                return image;

            }
            catch
            {
                return new BitmapImage();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
